package org.ring.common;

import java.net.MalformedURLException;

/**
 * // protocol://host:port?param1=value1
 *
 * @since 2024-10-29 17:15
 */
public class URL {

    private String protocol;

    private String host;

    private int port;

    public URL(String protocol, String host, int port) {
        this.protocol = protocol;
        this.host = host;
        this.port = port;
    }

    public static URL valueOf(String url) {
        int len = url.length();

        int protocolIndex = url.indexOf("://");
        if (protocolIndex == 0) {
            throw new IllegalStateException("no protocol: " + url);
        }
        int hostIndex = url.indexOf(':', protocolIndex + 3);
        String protocol = url.substring(0, protocolIndex);
        String host = url.substring(protocolIndex + 3, hostIndex);
        String portStr = url.substring(hostIndex + 1, len);
        int port = Integer.valueOf(portStr);
        return new URL(protocol, host, port);
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}
